package com.jiliang.jiliangserver.controller;


import com.jiliang.jiliangserver.domain.MeterQuery;
import com.jiliang.jiliangserver.domain.SysUser;
import com.jiliang.jiliangserver.domain.UserMeterDto;
import com.jiliang.jiliangserver.model.AjaxResult;
import com.jiliang.jiliangserver.model.RegisterBody;
import com.jiliang.jiliangserver.model.page.TableDataInfo;
import com.jiliang.jiliangserver.service.ISysUserService;
import com.jiliang.jiliangserver.utils.SecurityUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户信息
 * 
 * @author corner
 */
@RestController
@RequestMapping("/user")
public class SysUserController extends BaseController
{
    @Autowired
    private ISysUserService userService;

    /**
     * 获取用户列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysUser user)
    {
        startPage();
        List<SysUser> list = userService.selectUserList(user);
        return getDataTable(list);
    }

    /**
     * 根据用户编号获取详细信息
     */
    @GetMapping(value = { "/", "/{userId}" })
    public AjaxResult getInfo(@PathVariable(value = "userId", required = false) Long userId)
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put(AjaxResult.DATA_TAG, userService.selectUserById(userId));
        return ajax;
    }


    /**
     * 重置密码
     */
    @PutMapping("/resetPwd")
    public AjaxResult resetPwd(@RequestBody SysUser user)
    {
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        return toAjax(userService.resetPwd(user));
    }


    /**
     * 注册用户
     * @param registerBody
     * @return
     */
    @PostMapping("/register")
    public AjaxResult register(@RequestBody RegisterBody registerBody)
    {
        String register = userService.register(registerBody);
        return AjaxResult.success(register);
    }

    /**
     * 更新用户
     * @param user
     * @return
     */
    @PutMapping("/editUser")
    public AjaxResult editUser(@Validated @RequestBody SysUser user)
    {
        return toAjax(userService.updateUser(user));
    }

    /**
     * 绑定水表
     * @param user
     * @return
     */
    @PutMapping("/bindMeter")
    public AjaxResult bindMeter(@Validated @RequestBody SysUser user)
    {
        return toAjax(userService.bindMeter(user));
    }

    /**
     * 查询用户绑定水表信息
     * @param query
     * @return
     */
    @GetMapping("/selectUserMeterInfo")
    public AjaxResult selectUserMeterInfo(UserMeterDto query){
        List<Map> maps = userService.selectUserMeterInfo(query);
        return AjaxResult.success(maps);
    }



}
